<html>
<head><meta charset="utf-8"><title>Stability of BasicBlocks · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Stability.20of.20BasicBlocks.html">Stability of BasicBlocks</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234737894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Stability%20of%20BasicBlocks/near/234737894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Stability.20of.20BasicBlocks.html#234737894">(Apr 15 2021 at 19:28)</a>:</h4>
<p><a href="https://rustc-dev-guide.rust-lang.org/query.html">Queries: demand-driven compilation</a> explains "<em>big chunks of the compiler (for example, generating MIR) work exactly like this</em>".  But delving into that a bit, <code>BasicBlockData::terminator</code> can obviously include other <code>BasicBlock</code>s (which are just newtyped indexes into the <code>basic_blocks</code> vector)... so how stable is this?  If a subsequent compilation introduces a new block or removes an old one, might this cause significant movement in the <code>basic_blocks</code> vector such that all affected blocks (and any that reference them) are no longer "green" queries and have to be regenerated?  Or does the compiler try to preserve a block's index wherever possible?</p>



<a name="234738427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Stability%20of%20BasicBlocks/near/234738427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Stability.20of.20BasicBlocks.html#234738427">(Apr 15 2021 at 19:31)</a>:</h4>
<p>A MIR basic block is too small a data structure for incremental compilation. When generating MIR, either we load the full former MIR from cache, or we generate all of it.</p>



<a name="234738997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Stability%20of%20BasicBlocks/near/234738997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Stability.20of.20BasicBlocks.html#234738997">(Apr 15 2021 at 19:34)</a>:</h4>
<p><span class="user-mention silent" data-user-id="248906">cjgillot</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Stability.20of.20BasicBlocks/near/234738427">said</a>:</p>
<blockquote>
<p>A MIR basic block is too small a data structure for incremental compilation. When generating MIR, either we load the full former MIR from cache, or we generate all of it.</p>
</blockquote>
<p>"<em>the full former MIR</em>" ... of a function?  Or do you mean the whole crate??</p>



<a name="234739163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Stability%20of%20BasicBlocks/near/234739163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Stability.20of.20BasicBlocks.html#234739163">(Apr 15 2021 at 19:35)</a>:</h4>
<p>I guess it must be the whole crate.</p>



<a name="234739356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Stability%20of%20BasicBlocks/near/234739356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Stability.20of.20BasicBlocks.html#234739356">(Apr 15 2021 at 19:36)</a>:</h4>
<p>MIR is computed per-item</p>



<a name="234739933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Stability%20of%20BasicBlocks/near/234739933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Stability.20of.20BasicBlocks.html#234739933">(Apr 15 2021 at 19:40)</a>:</h4>
<p>Sorry, I overlooked that the <code>basic_blocks</code> vector was on <code>Body</code>.  That makes a lot more sense :) Thanks!</p>



<a name="235088120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Stability%20of%20BasicBlocks/near/235088120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Stability.20of.20BasicBlocks.html#235088120">(Apr 18 2021 at 17:46)</a>:</h4>
<p>Revisiting this having better grokked the dev guide and related docs, I'm a bit surprised to see that <code>BasicBlock</code> implements <code>HashStable</code>, when <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/stable_hasher/trait.HashStable.html">the documentation</a> quite understandably says:</p>
<blockquote>
<p><code>hash_stable()</code> must be independent of the current compilation session. E.g. they must not hash memory addresses or other things that are “randomly” assigned per compilation session.</p>
</blockquote>
<p>Surely a <code>BasicBlock</code> (being a mere index into a <code>basic_blocks</code> vector) is dependent on the compilation session?  If it's to implement <code>HashStable</code>, shouldn't something like the block's "normalized" path through the CFG be used instead?</p>



<a name="235088587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Stability%20of%20BasicBlocks/near/235088587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Stability.20of.20BasicBlocks.html#235088587">(Apr 18 2021 at 17:55)</a>:</h4>
<p>MIR building &amp; transformations are deterministic. The BasicBlocks are stable across compilation sessions.</p>



<a name="235088963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Stability%20of%20BasicBlocks/near/235088963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eggyal <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Stability.20of.20BasicBlocks.html#235088963">(Apr 18 2021 at 18:01)</a>:</h4>
<p>Ah, I see.  <em>Because</em> the MIR is either entirely loaded from cache or entirely regenerated (as <span class="user-mention" data-user-id="248906">@cjgillot</span> pointed out above), the <code>BasicBlock</code> indexes are "stable" in the sense that the <code>basic_blocks</code> vector is either reused or discarded... got it :) thanks</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>